package com.mc.dao.Impl;

import com.mc.bean.Jury;
import com.mc.dao.JuryDao;

import java.sql.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangcf
 * @author zl
 * @version 1.0
 * @Description 评委数据访问接口的实现类
 * @date 2023/2/13 9:44
 */
public class JuryDaoImpl extends BaseDao implements JuryDao {

    public Jury newJury() throws SQLException {
        Jury jury = new Jury(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getInt(5), rs.getString(6), rs.getString(7), rs.getInt(8));
        return jury;
    }

    @Override
    public List<Jury> findAll() {
        List<Jury> juryList = new ArrayList<>();
        try {
            //加载驱动
            regist();
            //连接数据库
            connection();
            //创建sql语句
            getPst("select * from jury");
            //添加结果集
            getRs();
            while (rs.next()){
                Jury jury=newJury();
                juryList.add(jury);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return juryList;
    }

    @Override
    public Jury findByJuryId(int juryid) {
        Jury jury=null;
        try {
            //加载驱动
            regist();
            //连接数据库
            connection();
            //创建sql语句
            getPst("select * from jury where juryid=?");
            pstmt.setInt(1,juryid);
            //添加结果集
            getRs();
            while (rs.next()){
                jury=newJury();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return jury;
    }

    @Override
    public Jury findByTelPass(String tel, String pass) {
        Jury jury=null;
        try {
            //加载驱动
            regist();
            //连接数据库
            connection();
            //创建sql语句
            getPst("select * from jury where jurytel=? and jurypassword=?");
            pstmt.setString(1,tel);
            pstmt.setString(2,pass);
            //添加结果集
            getRs();
            while (rs.next()){
                jury=newJury();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return jury;
    }

    @Override
    public int add(Jury jury) {
        int i=0;
        try {
            //加载驱动
            regist();
            //连接数据库
            connection();
            //创建sql语句
            getPst("insert into jury (juryname,jurytel,jurypassword,juryage,jurysex,juryheadurl,rid) values (?,?,?,?,?,?,?)");
            pstmt.setString(1,jury.getJuryname());
            pstmt.setString(2,jury.getJurytel());
            pstmt.setString(3,jury.getJurypassword());
            pstmt.setInt(4,jury.getJuryage());
            pstmt.setString(5,jury.getJurysex());
            pstmt.setString(6,jury.getJuryheadurl());
            pstmt.setInt(7,jury.getRid());
            i=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }

    @Override
    public int delete(int juryid) {
        int i=0;
        try {
            //加载驱动
            regist();
            //连接数据库
            connection();
            //创建sql语句
            getPst("delete from jury where juryid=? ");
            pstmt.setInt(1,juryid);
            i=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }

    @Override
    public int update(Jury jury) {
        int i=0;
        try {
            //加载驱动
            regist();
            //连接数据库
            connection();
            //创建sql语句
            getPst("update jury set juryname=?,jurytel=?,jurypassword=?,juryage=?,jurysex=?,juryheadurl=?,rid=? where juryid=?");
            pstmt.setString(1,jury.getJuryname());
            pstmt.setString(2,jury.getJurytel());
            pstmt.setString(3,jury.getJurypassword());
            pstmt.setInt(4,jury.getJuryage());
            pstmt.setString(5,jury.getJurysex());
            pstmt.setString(6,jury.getJuryheadurl());
            pstmt.setInt(7,jury.getRid());
            pstmt.setInt(8,jury.getJuryid());
            i=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }

    @Override
    public Jury findByTel(String tel) {
        Jury jury=null;
        try {
            //加载驱动
            regist();
            //连接数据库
            connection();
            //创建sql语句
            getPst("select * from jury where jurytel=?");
            pstmt.setString(1,tel);
            //添加结果集
            getRs();
            while (rs.next()){
                jury=newJury();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return jury;
    }
}
